<?php
require('config.php');

        $user_id=$_REQUEST['user_id'];
		$sql="select * from `".$oto."_users` where `userId`='{$user_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$time=time();
		$ktx_yj=$user_info[0]['agentBalance'];
		$sqz_yj=$user_info[0]['agentWaitPrice'];
		$ytx_yj=$user_info[0]['agentPayPrice'];
		$loginName=$user_info[0]['loginName'];
		$tel=$user_info[0]['userPhone'];
		$tx_yj=0;
		if($ktx_yj==0)
		{
			$data[0]['pan']=0;
			$data[0]['msg']="没有可提现佣金";
			echo json_encode($data);
			exit();
		}
		else
		{
			$sql="select * from `".$oto."_agent_apply` where `userId`='{$user_id}' and `status`='0'";
			$result=$db->query($sql);
			while($row=$result->fetch_assoc()){
				$isAgent[]=$row;//将取得的所有数据赋值给person_info数组
			}
			if(isset($isAgent))
			{
				$data[0]['pan']=0;
				$data[0]['msg']="已有申请待处理中";
				echo json_encode($data);
				exit();
			}
			$sql="INSERT INTO ".$oto."_agent_apply (`userId`,`loginName`,`applyType`,`userType`,`tel`,`applyPrice`,`status`,`time`) VALUES ('$user_id','$loginName','1','0','$tel','$ktx_yj','0','$time')";
            $result=$db->query($sql);
            $payprice=$ytx_yj+$ktx_yj;
            $user_money=$user_info[0]['userMoney']+$ktx_yj;
   //          $sql="update ".$oto."_users set agentBalance='0',agentPayPrice='{$payprice}',userMoney='{$user_money}' where userId='{$user_id}'";
			// $result=$db->query($sql);
			$sql="update ".$oto."_users set agentBalance='0',agentPayPrice=agentPayPrice+'$ktx_yj',agentWaitPrice=agentWaitPrice+'$ktx_yj' where userId='{$user_id}'";
			$result=$db->query($sql);
			// $sql="INSERT INTO ".$oto."_money_record (`type`,`money`,`time`,`IncDec`,`userid`,`balance`,`remark`,`payWay`) VALUES ('4','$ktx_yj','$time','1','$user_id','$user_money','佣金提现','0')";
   //          $result=$db->query($sql);
			$data[0]['pan']=1;
		}
		echo json_encode($data);
?>